#include "login.h"
#include "ui_login.h"

#include "widget_1.h"
#include "ui_widget_1.h"


void login::sql_init(QString Path)
{
    // 1、连接数据库
    db = QSqlDatabase::addDatabase("QSQLITE"); // 使用 SQLite 驱动
    db.setDatabaseName(Path);            // 设置数据库文件路径
    if (!db.open()) {
        qDebug() << "数据库打开失败!" << db.lastError().text();
        return;
    }
    qDebug() << "连接成功!";

    // 初始化查询对象
    query = QSqlQuery(db);

    // 2、创建数据表
    if (!query.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, user TEXT, password TEXT)")) {
        qDebug() << "创建表格失败" << query.lastError().text();
        return;
    }
    qDebug() << "数据表创建成功!";
}


// 登录
void login::on_Log_ing_user()
{
    // 执行查询
    if (!query.exec("SELECT * FROM users")) {
        qDebug() << "查询失败!" << query.lastError().text();
        return;
    }
    // 遍历数据表 查询账号密码是否正确
    while (query.next()) {
        int id = query.value("id").toInt();
        QString user = query.value("user").toString();
        QString password = query.value("password").toString();
        qDebug() << "ID:" << id << ", user:" << user << ", password:" << password;
        // ;
        // ;

        if(user == this->ui->lineEdit_user->text() && password == this->ui->lineEdit_passwrod->text())
        {
            qDebug() << "登录成功";
            QMessageBox::information( this , "登录成功" , "欢迎回家!!!" );
            emit log_in_yes(user);
            return ;
        }
    }
    qDebug() <<  this->ui->lineEdit_user->text() <<  this->ui->lineEdit_passwrod->text() ;
    QMessageBox::question(this , "登录失败" , "账号或密码错误" , QMessageBox::Ok);
}

// 注册
void login::on_reg_user()
{
    QString user = this->ui->lineEdit_user->text();
    QString password = this->ui->lineEdit_passwrod->text();
    // 执行查询
    if (!query.exec("SELECT * FROM users")) {
        qDebug() << "查询失败!" << query.lastError().text();
        return;
    }

    // 遍历数据表 查询账号密码是否正确
    while (query.next()) {
        int id = query.value("id").toInt();
        QString user = query.value("user").toString();
        QString password = query.value("password").toString();
        qDebug() << "ID:" << id << ", user:" << user << ", password:" << password;
        if(user == this->ui->lineEdit_user->text() )
        {
            qDebug() << "已有重名用户";
            QMessageBox::information( this , "注册错误" , "已有重名用户 , 请重新填写 " );
            return ;
        }
    }

    // 无重名用户

    // 准备 SQL 插入语句
    query.prepare("INSERT INTO users (user, password) VALUES (:user, :password)");
    query.bindValue(":user", user);
    query.bindValue(":password", password);


    // 执行插入操作
    if (!query.exec()) {
        qDebug() << "插入失败!" << query.lastError().text();
        return;
    }
    qDebug() << "数据插入成功!";

    QMessageBox::information( this , "注册成功" , "恭喜你！！！" );
}


void widget_1::sql_init(QString Path)
{
    // 1、连接数据库
    db = QSqlDatabase::addDatabase("QSQLITE"); // 使用 SQLite 驱动
    db.setDatabaseName(Path);            // 设置数据库文件路径
    if (!db.open()) {
        qDebug() << "数据库打开失败!" << db.lastError().text();
        return;
    }
    qDebug() << "连接成功!";

    // 初始化查询对象
    query = QSqlQuery(db);

    // 2、创建数据表
    if (!query.exec("CREATE TABLE IF NOT EXISTS media (id INTEGER PRIMARY KEY, name TEXT, Url TEXT UNIQUE)")) {
        qDebug() << "创建表格失败" << query.lastError().text();
        return;
    }
    qDebug() << "数据表创建成功!";
}

bool widget_1::add_Media_url(QUrl file_url)
{
    // 无重名用户

    // 准备 SQL 插入语句
    query.prepare("INSERT INTO media (name, url) VALUES (:name, :url)");
    query.bindValue(":name", file_url.fileName());
    query.bindValue(":url", file_url.toString());


    // 执行插入操作
    if (!query.exec()) {
        qDebug() << "插入失败!" << query.lastError().text();
        return  false;
    }
    qDebug() << "数据插入成功!";

   // QMessageBox::information( this , "注册成功" , "恭喜你！！！" );
    return  true;
}

void widget_1::up_data_list()
{

    if (!query.exec("SELECT * FROM media")) {
        qDebug() << "查询失败!" << query.lastError().text();
        return;
    }
    // 遍历数据表 查询歌曲是否存在
    while (query.next())
    {
        int ID =  query.value("ID").toInt();
        QString MediaName_temp = query.value("name").toString();
        QUrl url = query.value("url").toUrl();

        QListWidgetItem *item_temp = new QListWidgetItem(MediaName_temp);
        listWidget->addItem(item_temp);
        qDebug() << "ID" << ID <<"MediaName:" << MediaName_temp << ", url:" << url;


    }return ;
}

Mediatype widget_1::select_MediaUrlByname(QString MediaName)
{
    Mediatype  Mediatemp ={0,"",QUrl("")};
    if (!query.exec("SELECT * FROM media")) {
        qDebug() << "查询失败!" << query.lastError().text();
        return  play_Media;
    }
    // 遍历数据表 查询歌曲是否存在
    while (query.next())
    {
        int ID =  query.value("ID").toInt();
        QString MediaName_temp = query.value("name").toString();
        QUrl url = query.value("url").toUrl();

        if(MediaName == MediaName_temp)
        {
          Mediatemp.ID =ID;

          Mediatemp.name = MediaName_temp;
          Mediatemp.Path = url;
          qDebug() << "MediaName:" << MediaName << ", url:" << url ;
          return Mediatemp;
        }
    }

    return play_Media;
}

Mediatype widget_1::select_MediaUrlID(int ID)
{
    Mediatype  Mediatemp ={0,"",QUrl("")};
    if (!query.exec("SELECT * FROM media")) {
        qDebug() << "查询失败!" << query.lastError().text();
        return  play_Media;
    }
    // 遍历数据表 查询歌曲是否存在
    while (query.next())
    {
        int ID_temp =  query.value("ID").toInt();
        QString MediaName_temp = query.value("name").toString();
        QUrl url = query.value("url").toUrl();
        if(ID == ID_temp)
        {
          Mediatemp.ID =ID;
          Mediatemp.name = MediaName_temp;
          Mediatemp.Path = url;
          qDebug() << "ID"<<ID<< "MediaName:" << MediaName_temp << ", url:" << url ;
          return Mediatemp;
        }
    }
    Mediatemp ={0,"",QUrl("")};
    return play_Media;
}

